<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-button type="primary" size="small" @click="addRole">添加角色</el-button>
        </el-row>
        <el-divider />
        <el-table :data="roleList" border>
          <el-table-column label="角色名称" prop="title" />
          <el-table-column label="角色描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary" size="small" @click="assignPerm(row.id)">分配权限</el-button>
              <el-button size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加角色组件 -->
      <addRole
        ref="addRole"
        :show-dialog.sync="showDialog"
        @updateEvent="getRoleList"
      />
      <!-- 分配权限 -->
      <AssignPerm ref="assignPerm" :show-dialog.sync="visibleDialog" />
    </div>
  </div>
</template>

<script>
import { roleList, delRole } from '@/api/roles/index'
import addRole from './components/addRole.vue'
import AssignPerm from './components/assign-perm.vue'
export default {
  components: { addRole, AssignPerm },
  data() {
    return {
      roleList: [], // 角色列表
      showDialog: false, // 弹窗开关
      visibleDialog: false// 权限弹窗开关
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 分配权限
    assignPerm(id) {
      this.visibleDialog = true
      this.$refs.assignPerm.getRolePermIds(id)
    },
    // 编辑
    editFn(id) {
      this.showDialog = true
      console.log(this.$refs.addRole)
      this.$refs.addRole.getRoleDetail(id)
    },
    // 删除角色
    delFn(id) {
      this.$confirm('是否确定删除？', '提示', {
        type: 'warning'
      }).then(async() => {
        await delRole(id)
        this.getRoleList()
      }).catch((err) => {
        console.log('点击了取消按钮', err)
      })
    },
    // 新增角色
    addRole() {
      this.showDialog = true
    },
    // 获取角色列表
    async getRoleList() {
      const res = await roleList()
      // console.log(res)
      this.roleList = res.data
    }
  }
}
</script>

<style>

</style>
